package com.example.seven.mynews.login;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.app.LoaderManager.LoaderCallbacks;

import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.example.seven.mynews.R;
import com.example.seven.mynews.activity.MainActivity;
import com.example.seven.mynews.data.MyDB;

import static android.Manifest.permission.READ_CONTACTS;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends AppCompatActivity{
    private EditText username;
    private EditText password;
    private MyDB myDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        myDB = new MyDB(this,"mydb");
        myDB.open();
        username = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);

    }

    public void myClick(View view){
        switch (view.getId()){
            case R.id.clear:
                username.setText("");
                password.setText("");
                break;
            case R.id.register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                String name = username.getText().toString();
                String aPassword = password.getText().toString();
                if (name.isEmpty()||name.equals("")||aPassword.isEmpty()||aPassword.equals("")){
                    Toast.makeText(this,"用户名或者密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    boolean isLogin= myDB.login(name,aPassword);
                    if (isLogin){
                        Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
                        SharedPreferences sharedPreferences = getSharedPreferences("xy",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("hasLogin",true);//更改登陆状态
                        editor.commit();
                        Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent1);
                        //finish();
                    }else {
                        Toast.makeText(this,"用户名或者密码错误",Toast.LENGTH_SHORT).show();
                    }
                }

        }
        }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myDB.close();
    }
}

